// This file is a part of ChaosDB.
// Copyright (C) 2012 ChaoJianPeng
// email:chaojianpeng@gmail.com
//
// ChaosDB is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// ChaosDB is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/*
 * hashmap.cpp
 *
 *  Created on: 2012-3-4
 *      Author: chao
 */

#include "hashmap.h"
#include "uthash.h"
#include "cmalloc.h"

Entry* entryCreat(char* key, void* value) {
	Entry* entry;
	entry = (Entry*) cmalloc(sizeof(Entry));
	entry->key = key;
	entry->value = value;
	entry->lruLeft = entry->lruRight = NULL;
	return entry;
}

void entryDel(Entry* entry) {
	if (entry->key != NULL) {
		cfree(entry->key);
		entry->key = NULL;
	}
	if (entry->value != NULL) {
		cfree(entry->value);
		entry->value = NULL;
	}
	entry->lruLeft = entry->lruRight = NULL;
}

void entryRelease(Entry* entry) {
	// 是释放value
	if (entry->value != NULL) {
		cfree(entry->value);
		entry->value = NULL;
	}
}

HashMap* hmCreat() {
	HashMap* hm = (HashMap*)malloc(sizeof(HashMap));
	hm->ht = NULL;
	return hm;
}
int hmSize(HashMap* hm) {
	return HASH_COUNT(hm->ht);
}

Entry* hmHead(HashMap* hm) {
	return hm->ht;
}

Entry* hmAdd(HashMap* hm, char* key, void* value, int len) {
	Entry* entry = NULL;
	HASH_FIND_STR(hm->ht, key, entry);
	if (entry != NULL) {
		return NULL;
	}
	entry = entryCreat(key, value);
	HASH_ADD_KEYPTR(hh, hm->ht, key, len, entry);
	return entry;
}

Entry* hmSet(HashMap* hm, char* key, void* value, int len) {
	Entry* entry;
	HASH_FIND_STR(hm->ht, key, entry);
	if (entry != NULL) {
		entry = entryCreat(key, value);
		HASH_ADD_KEYPTR(hh, hm->ht, key, len, entry);
		return entry;
	} else {
		// 清空原有内存
		cfree(key);
		if(entry->value!=NULL){
			cfree(entry->value);
		}
		entry->value = value;
		return entry;
	}

}

Entry* hmGet(HashMap* hm, char* key) {
	Entry* entry;
	HASH_FIND_STR(hm->ht, key, entry);
	return entry;
}

/**
 * 未释放内存需要手动释放
 */
Entry* hmDel(HashMap* hm, char* key) {
	Entry* entry;
	HASH_FIND_STR(hm->ht, key, entry);
	if (entry == NULL) {
		return NULL;
	}
	HASH_DEL(hm->ht, entry);
//	entry->del();
//	cfree(entry);
	return entry;
}

void hmClear(HashMap* hm) {
	Entry* current_entry;
	Entry* tmp;
	HASH_ITER(hh, hm->ht, current_entry, tmp) {
		HASH_DEL(hm->ht, current_entry);
		entryDel(current_entry);
		cfree(current_entry);
	}
}

Entry* hmReplace(HashMap* hm, char* key, void* value, int len) {
	Entry* entry;
	HASH_FIND_STR(hm->ht, key, entry);
	if (entry == NULL) {
		return NULL;
	}
	// 清空原有内存
	if(entry->value!=NULL){
		cfree(entry->value);
	}
	entry->value = value;
//	HASH_ADD_KEYPTR(hh, hm->ht, key, len, entry);
	return entry;
}
